#id1 前，default=0; id2 后，default=last.id; pa, 必须有except.yml
namespace :bld do
  desc "Export"
  task :export => :environment do
    puts time = Time.now
    pa = ENV['pa'] ? "#{BILI_PRO_PATH}#{ENV['pa']}/" : "#{BILI_PRO_PATH}ranking/"
    id1 = ENV['id1'] || (YAML.load(open(File.join "#{Rails.root}/config/last.yml")) rescue 0)
    id2 = ENV['id2'] || Scan.last.id
    except_ids = pa ? YAML.load(open(File.join(pa + "except.yml"))) : [-1]
    #except_ids << Weekly.latest.wid unless id1.zero?
    #=====================================================================
=begin
    doc = File.new(pa ? pa + "_list.csv" : "#{RAILS_ROOT}/_list.csv", 'w')
    str = "总分排名,总分,id,投稿时间,标题,类别,投稿人,点击,点击排名,评论,评论排名,收藏,收藏排名,弹幕,弹幕排名,P,附加修正\n"
=end
    #all_work_ids = Work.find(:all, :select => "wid", :conditions => ["status = 1 and wid not in (?) and ac_type_id != 18", except_ids]).collect{|w| w.wid} unless ENV['type']
    weekly_results = WeeklyResult.stat(id1, id2, except_ids, (ENV['type'] || :all))
    File.open(%[#{Rails.root}/config/_list.yml],"w"){ |io| YAML.dump weekly_results[0...200],io }
    #all_work_ids -= weekly_results.collect{|r| r.work.wid} unless ENV['type']
=begin
    weekly_results.select{|r| r['point'] >= 1000 || r['point'] < 0}.each do |r|
        str << %(#{r['point_rank']},#{r['point']},)
        str << %(#{r.work.wid},#{r.work.cdate},"#{r.work.name.gsub(/"/, '""')}",#{r.work.ac_type.name},"#{r.work.author_name.gsub(/"/, '""') rescue nil}",)
        str << %(#{r.clicks},#{r['click_rank']},#{r.comments},#{r['comment_rank']},)
        str << %(#{r.stows},#{r['stow_rank']},)
        str << %(#{r.danmu},#{r['danmu_rank']},)
        str << %(#{r.work.part_count},#{r.space_fix})
        str << "\n"
    end
    doc.write str
=end
    puts "#{Time.now - time} second complete"

    #unless ENV['type']
     # File.open(%[#{Rails.root}/config/_lost.yml],"w"){ |io| YAML.dump all_work_ids.select{|id| id < Work.maximum('wid')},io }
     # Work.find(:all, :conditions => ["wid in (?)", all_work_ids.select{|id| id > Work.maximum('wid') - 10000}]).each{|w| w.get_info;sleep(0.3)}
    #end
  end
end
